﻿#region Copyright ©2008-2011, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: then $
// Last Changed Date     $LastChangedDate: 2009-03-19 13:58:26 +0100 (Thu, 19 Mar 2009) $
// Last Changed Revision $Rev: 42 $
//
// ====================================================================================================
#endregion

using System.IO;
using System.Windows;
using System.Windows.Media;
using Tum.CollabXT.TFS.Properties;
using System.Windows.Forms;
using System.Windows.Controls;

namespace Tum.CollabXT.TFS
{
    /// <summary>
    /// Interaction logic for TFSOutputPage.xaml
    /// </summary>
    public partial class TFSOutputPage : TFSPage
    {
        TFSToolProvider ToolProvider;
        IConversionWorkflow ConversionWorkflow;


        /// <summary>
        /// Constructor.
        /// </summary>
        public TFSOutputPage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handler used to pass the provider and workflow handles to this page.
        /// </summary>
        internal override void ReceiveHandles(TFSToolProvider toolProvider, IConversionWorkflow conversionWorkflow)
        {
            ToolProvider = toolProvider;
            ConversionWorkflow = conversionWorkflow;

            SetValues();
        }

        /// <summary>
        /// Handler used when the user wants to change the current workflow page.
        /// </summary>
        internal override bool ChangePage(bool advance)
        {
            if (!advance)
                return true; //Return to previous page is always possible

            if (ToolProvider != null)
                return ToolProvider.OutputPath != null;
            
            return true; //Let user go to next page even though tp handle ist not set
        }

        /// <summary>
        /// Updates the 'enabled' status of the 'prev'  and 'next' buttons according to the data provided by the user.
        /// </summary>
        private void UpdateWorkflowButtons()
        {
            if (ToolProvider != null)
            {
                ConversionWorkflow.UpdateButtonState(true, ToolProvider.OutputPath != null);
            } 
        }

        /// <summary>
        /// Updates the user interface to represent the values saved by the ToolProvider object.
        /// </summary>
        private void SetValues()
        {
            //Output path
            if (ToolProvider.OutputPath == null)
                LabelOutputPathValue.Content = TFS.Resources.Language.PathNotSet;
            else
            {
                LabelOutputPathValue.Content = ToolProvider.OutputPath.Replace('/', '\\');
                LabelOutputPathValue.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
            }

            //Button states
            UpdateWorkflowButtons();
        }

        /// <summary>
        /// Handler for the click on the 'browse' button.
        /// </summary>
        private void ButtonSetOutputPath_Click(object sender, RoutedEventArgs e)
        {
            using (FolderBrowserDialog fbd = new FolderBrowserDialog())
            {
                fbd.ShowNewFolderButton = true;

                if (Directory.Exists(Settings.Default.LastOutputPath))
                    fbd.SelectedPath = Settings.Default.LastOutputPath;

                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    if (!fbd.SelectedPath.Equals(ToolProvider.OutputPath))
                    {
                        ToolProvider.OutputPath = fbd.SelectedPath;
                        ConversionWorkflow.InvalidateProcessing();
                        SetValues();

                        Settings.Default.LastOutputPath = fbd.SelectedPath;
                        Settings.Default.Save();
                    }
                }
            }
        }
    }
}
